from django.db import models
from django.contrib.auth.models import AbstractUser


# Create your models here.

# 创建trucks，车辆表
class trucks(models.Model):
    truck_id = models.CharField(max_length=3, primary_key=True)

    truck_pattern = models.CharField(max_length=4)


# 创建drivers，车辆表
class drivers(models.Model):
    driver_id = models.CharField(max_length=3, primary_key=True)

    driver_name = models.CharField(max_length=20)

    driver_phone = models.CharField(max_length=11)

    driver_health = models.CharField(max_length=20)

    driver_level = models.SmallIntegerField()

    driver_det = models.TextField()


# 创建avai_trucks,可用车辆表
class avai_trucks(models.Model):
    truck = models.OneToOneField('trucks', to_field='truck_id', on_delete=models.CASCADE, primary_key=True)

    driver = models.ForeignKey('drivers', to_field='driver_id', on_delete=models.CASCADE)

    truck_pattern = models.CharField(max_length=4)

    truck_volume = models.IntegerField()

    truck_year = models.IntegerField()

    truck_status = models.CharField(max_length=10)

    # 除菌设备状态
    se_status = models.CharField(max_length=20)

    # 制冷设备状态
    con_status = models.CharField(max_length=20)

    truck_loc = models.CharField(max_length=25)


# 建立scrapped_trucks，报废车辆表
class scrapped_trucks(models.Model):
    truck = models.OneToOneField('trucks', to_field='truck_id', on_delete=models.CASCADE, primary_key=True)

    truck_pattern = models.CharField(max_length=4)

    # 报废去向
    scrapped_dest = models.CharField(max_length=40)

    # 报废备注
    scrapped_det = models.CharField(max_length=128)


# 建立service_trucks，维修车辆表
class service_trucks(models.Model):
    truck = models.OneToOneField('trucks', to_field='truck_id', on_delete=models.CASCADE, primary_key=True)

    truck_pattern = models.CharField(max_length=4)

    service_status = models.CharField(max_length=8)

    # 维修备注
    service_det = models.TextField()


# 建立statistics_trucks，车辆统计表
class statistics_trucks(models.Model):
    pattern_id = models.CharField(max_length=3, primary_key=True)

    truck_pattern = models.CharField(max_length=4)

    truck_num = models.PositiveSmallIntegerField()

    # Django不支持双主键，多主键，因此不考虑采用原方案，设置统计时间和id作为主键
    statistics_time = models.DateTimeField()


# 建立truck_demands,车辆需求表
class truck_demands(models.Model):
    demand_id = models.CharField(max_length=4, primary_key=True)

    # 产生此条需求的时间
    demand_time = models.DateTimeField(auto_now_add=True)

    demand_pattern = models.CharField(max_length=4)

    demand_num = models.PositiveSmallIntegerField()

    is_processed = models.BooleanField()

    # 需求处理记录
    processs_rec = models.TextField()


# 建立warehouse，仓库表
class warehouse(models.Model):
    warehouse_id = models.CharField(max_length=4, primary_key=True)

    w_address = models.CharField(max_length=256)

    # 仓库维度，用x表示可能不太专业？
    w_latitude = models.FloatField()

    # 仓库经度
    w_longtitude = models.FloatField()

    # 货架数量
    w_n_shelf = models.PositiveSmallIntegerField()

    # 租金
    w_rent_fee = models.PositiveSmallIntegerField()

    # 到期日
    w_date = models.DateField()


# 建立shelf,仓库货架信息表
class shelf(models.Model):
    s_id = models.CharField(max_length=9, primary_key=True)

    is_available = models.BooleanField()

    volume = models.PositiveSmallIntegerField()

    is_cold = models.BooleanField()

    s_x = models.PositiveSmallIntegerField()

    s_y = models.PositiveSmallIntegerField()

    volume_used = models.PositiveSmallIntegerField()
    warehouse = models.ForeignKey(warehouse, on_delete=models.CASCADE)


# 建立invoice,发货单表
class invoice(models.Model):
    invoice_id = models.CharField(max_length=6, primary_key=True)

    # 仓库地址
    invent_loc = models.CharField(max_length=256)

    # 运送目的地
    dest_loc = models.CharField(max_length=256)

    # 发货产品
    invoice_prod = models.ForeignKey('product', on_delete=models.CASCADE)

    invoice_num = models.PositiveSmallIntegerField()

    invoice_time = models.DateTimeField()

    invoice_det = models.TextField()

    is_tasked = models.BooleanField(default=0)


# 建立shiptask，运送任务表
class shiptask(models.Model):
    task_id = models.CharField(max_length=6, primary_key=True)

    invoice = models.ForeignKey('invoice', to_field='invoice_id', on_delete=models.CASCADE)

    invent_loc = models.CharField(max_length=256)

    dest_loc = models.CharField(max_length=256)

    task_time = models.DateTimeField()

    task_guide = models.TextField()

    task_det = models.TextField()

    is_schedule = models.BooleanField(default=0)


# 建立schedule，调度表
class schedule(models.Model):
    schedule_id = models.CharField(max_length=4, primary_key=True)

    task = models.ForeignKey('shiptask', to_field='task_id', on_delete=models.CASCADE)

    truck = models.ForeignKey('trucks', to_field='truck_id', on_delete=models.CASCADE)

    schedule_route = models.TextField()

    schedule_status = models.CharField(max_length=8)


# 建立abnormal，异常通知表
class abnormal(models.Model):
    abnormal_id = models.CharField(max_length=4, primary_key=True)

    schedule = models.ForeignKey('schedule', to_field='schedule_id', on_delete=models.CASCADE)

    abnormal_reas = models.CharField(max_length=256)

    abnormal_ispro = models.BooleanField()

    abnormal_time = models.DateTimeField()


# 建立产品表
class product(models.Model):
    p_ID = models.CharField(max_length=8, primary_key=True)

    p_name = models.CharField(max_length=20)

    p_type = models.CharField(max_length=1)

    p_package = models.CharField(max_length=10)

    p_price = models.FloatField()

    p_expiration = models.PositiveSmallIntegerField()

    p_weight = models.PositiveSmallIntegerField()

    p_cold = models.BooleanField()

    p_product = models.CharField(max_length=4)

    p_height = models.PositiveSmallIntegerField()

    p_width = models.PositiveSmallIntegerField()

    p_length = models.PositiveSmallIntegerField()


# 库存信息
class storage(models.Model):
    s_order_ID = models.CharField(max_length=20)
    p_ID = models.ForeignKey('product', to_field='p_ID', on_delete=models.CASCADE)
    p_name = models.CharField(max_length=20)
    p_type = models.CharField(max_length=1)
    p_package = models.CharField(max_length=10)
    s_number = models.SmallIntegerField()
    product_date = models.DateField()
    reach_date = models.DateField(auto_now_add=True)
    expired_date = models.DateField()
    expired = models.BooleanField()
    warehouse = models.ForeignKey(warehouse, on_delete=models.CASCADE)
    shelf = models.ForeignKey(shelf, on_delete=models.CASCADE)

    class Meta:
        unique_together = (("s_order_ID", "p_ID"),)

    @property
    def type_str(self):
        type_dict = {"1": "巴氏消毒奶", "2": "脱脂奶", "3": "常温奶", "4": "高钙牛奶", "5": "生鲜牛奶", "6": "灭菌牛奶",
                     "7": "无抗奶", "8": "调味奶"}
        # return type_dict[self.p_type] + "-" + self.p_package
        return type_dict[self.p_type]


# 入库清单
class Accept(models.Model):
    s_order_ID = models.CharField(max_length=20)
    product_date = models.DateField()
    reach_date = models.DateField(auto_now_add=True)
    number = models.SmallIntegerField()
    p_ID = models.ForeignKey('product', to_field='p_ID', on_delete=models.CASCADE)

    class Meta:
        unique_together = (("s_order_ID", "p_ID"),)


# 仓库流水
class Flow(models.Model):
    f_id = models.CharField(max_length=20)
    p_ID = models.ForeignKey('product', to_field='p_ID', on_delete=models.CASCADE)
    p_number = models.SmallIntegerField()
    f_type = models.CharField(max_length=1)
    f_time = models.DateTimeField(auto_now_add=True)
    memo = models.CharField(max_length=256)
    f_warehouse = models.ForeignKey('warehouse', to_field="warehouse_id", on_delete=models.CASCADE)

    class Meta:
        unique_together = (("f_id", "p_ID"),)


# 用户
class User(AbstractUser):
    u_name = models.CharField(max_length=8)
    u_occupation = models.CharField(max_length=20)
    u_address = models.CharField(max_length=100)
    u_work_place = models.CharField(max_length=100)
    u_phone = models.CharField(max_length=15)
    u_warehouse = models.ForeignKey(warehouse, on_delete=models.SET_NULL, null=True)


# 通知消息
class note_info(models.Model):
    n_type = models.CharField(max_length=1)
    n_content = models.CharField(max_length=256)
    n_url = models.CharField(max_length=256)
    n_serious = models.CharField(max_length=1)
    n_receiver = models.ForeignKey(User, related_name="receiver", on_delete=models.CASCADE)
    n_creator = models.ForeignKey(User, related_name="creator", on_delete=models.SET_NULL, null=True)
    n_time = models.DateTimeField(auto_now_add=True)


# 供应商
class supplier(models.Model):
    sup_id = models.CharField(max_length=4, primary_key=True)
    sup_name = models.CharField(max_length=20)
    sup_location = models.CharField(max_length=100)
    sup_cert = models.CharField(max_length=1)
    sup_credit = models.CharField(max_length=1)


# 客户
class customer(models.Model):
    c_id = models.CharField(max_length=4, primary_key=True)
    c_name = models.CharField(max_length=20)
    c_address = models.CharField(max_length=100)
    c_longitude = models.FloatField()
    c_latitude = models.FloatField()
    c_level = models.CharField(max_length=2)
    c_credit = models.CharField(max_length=1)


# 销售订单
class SaleOrder(models.Model):
    o_id = models.CharField(max_length=8, primary_key=True)
    start_time = models.DateTimeField(auto_now_add=True)
    customer = models.ForeignKey(customer, on_delete=models.CASCADE)
    money = models.FloatField()
    memo = models.CharField(max_length=256)
    status = models.CharField(max_length=256)


# 销售详细信息
class SaleInfo(models.Model):
    o_id = models.ForeignKey(SaleOrder, on_delete=models.CASCADE)
    p_id = models.ForeignKey(product, on_delete=models.CASCADE)
    number = models.SmallIntegerField()
    price = models.FloatField()

    class Meta:
        unique_together = (("o_id", "p_id"),)


# 商品需求表
class Demand(models.Model):
    number = models.CharField(max_length=20, primary_key=True)
    datetime = models.DateTimeField(auto_now_add=True)
    isSolved = models.BooleanField()
    warehouse = models.ForeignKey(warehouse, on_delete=models.CASCADE)


# 商品需求详细表
class DemandDetail(models.Model):
    demand = models.ForeignKey(Demand, on_delete=models.CASCADE)
    product = models.ForeignKey(product, on_delete=models.CASCADE)
    quantity = models.PositiveSmallIntegerField()
    remain = models.PositiveSmallIntegerField()


# 采购订单表
class PurchaseOrder(models.Model):
    number = models.CharField(max_length=20, primary_key=True)
    staff = models.ForeignKey(User, on_delete=models.CASCADE)
    warehouse = models.ForeignKey(warehouse, on_delete=models.CASCADE)
    supplier = models.ForeignKey(supplier, on_delete=models.CASCADE)
    datetime = models.DateTimeField(auto_now_add=True)
    total = models.FloatField()  # 订单总价
    isReceived = models.BooleanField()
    isPaid = models.BooleanField()
    isClosed = models.BooleanField()
    receive_time = models.DateTimeField(null=True)
    pay_time = models.DateTimeField(null=True)
    close_time = models.DateTimeField(null=True)


# 采购订单详细表
class PurchaseDetail(models.Model):
    purchase_order = models.ForeignKey(PurchaseOrder, on_delete=models.CASCADE)
    product = models.ForeignKey(product, on_delete=models.CASCADE)
    quantity = models.PositiveSmallIntegerField()
    price = models.FloatField()
